package com.ruoyi.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.charge.domain.WxUser;
import com.ruoyi.charge.mapper.WxUserMapper;
import com.ruoyi.common.enums.DeletedStatus;
import com.ruoyi.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.charge.bo.RecordAddBo;
import com.ruoyi.charge.bo.RecordQueryBo;
import com.ruoyi.charge.bo.RecordEditBo;
import com.ruoyi.charge.domain.Record;
import com.ruoyi.charge.mapper.RecordMapper;
import com.ruoyi.charge.vo.RecordVo;
import com.ruoyi.charge.service.IRecordService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 充电记录Service业务层处理
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Autowired
    private WxUserMapper wxUserMapper;

    @Override
    public RecordVo queryById(Long id) {
        Record db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, RecordVo.class);
    }

    @Override
    public List<RecordVo> queryList(RecordQueryBo bo) {
        if (StringUtils.isBlank(bo.getOpenid())) {
            throw new CustomException("openid 不能为空!");
        }
        String openid = bo.getOpenid();
        WxUser wxUser = wxUserMapper.selectOne(
                new LambdaQueryWrapper<WxUser>().eq(WxUser::getOpenid, openid)
                        .eq(WxUser::getDeleted, DeletedStatus.VALID.getCode())
                        .last("limit 1")
        );
        if (wxUser == null) {
            log.error("获取充电记录 openid 不存在！openid: " + openid);
            throw new CustomException("openid 不存在！");
        }
        LambdaQueryWrapper<Record> lqw = Wrappers.lambdaQuery();
        lqw.eq(Record::getUserId, wxUser.getWxUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getStoreNumber()), Record::getStoreNumber, bo.getStoreNumber());
        lqw.eq(StrUtil.isNotBlank(bo.getEquipmentNumber()), Record::getEquipmentNumber, bo.getEquipmentNumber());
        lqw.eq(Record::getStatus, 1);
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<RecordVo> entity2Vo(Collection<Record> collection) {
        List<RecordVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, RecordVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<Record> page = (Page<Record>) collection;
            Page<RecordVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(RecordAddBo bo) {
        Record add = BeanUtil.toBean(bo, Record.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(RecordEditBo bo) {
        Record update = BeanUtil.toBean(bo, Record.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Record entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<RecordVo> queryList2(RecordQueryBo bo) {
        LambdaQueryWrapper<Record> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, Record::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getStoreNumber()), Record::getStoreNumber, bo.getStoreNumber());
        lqw.eq(StrUtil.isNotBlank(bo.getEquipmentNumber()), Record::getEquipmentNumber, bo.getEquipmentNumber());
        lqw.eq(Record::getStatus, 1);
        return entity2Vo(this.list(lqw));
    }
}
